package com.ymatou.iapi.prodstock.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.prodstock.parameter.GetCatalogStockByProductInActivityIdListBean;
import com.ymatou.iapi.prodstock.service.GetCatalogStockByProductInActivityIdListCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 通过商品活动编号列表查询规格活动库存列表Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetCatalogStockByProductInActivityIdList {
	private static GetCatalogStockByProductInActivityIdListBean getcatalogstockbyproductinactivityidlistBean;
	private static GetCatalogStockByProductInActivityIdListCall getcatalogstockbyproductinactivityidlistCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通过商品活动编号列表查询规格活动库存列表Test");
	}

	@Before
	public void caseUp() {
		getcatalogstockbyproductinactivityidlistBean = new GetCatalogStockByProductInActivityIdListBean();
		getcatalogstockbyproductinactivityidlistCall = new GetCatalogStockByProductInActivityIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcatalogstockbyproductinactivityidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogStockByProductInActivityIdList_001() {
		Logger.start(true, "查询规格活动库存,单规格报名");
		try {
			// Ts_GetCatalogStockByProductInActivityId
			// 创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 创建一个活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);

			getcatalogstockbyproductinactivityidlistBean
					.setProductInActivityIdList(Arrays.asList(Integer.parseInt(aids.get(0))));
			getcatalogstockbyproductinactivityidlistCall.setData(getcatalogstockbyproductinactivityidlistBean);
			getcatalogstockbyproductinactivityidlistCall.callService();
			Logger.verifyEquals("200", getcatalogstockbyproductinactivityidlistCall.getString("Code"), "验证code");
			
			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(x -> {
				JsonObject CatalogStockMap = getcatalogstockbyproductinactivityidlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonObject("CatalogStockMap");
				JsonObject p = CatalogStockMap.getAsJsonArray(aids.get(0)).get(0).getAsJsonObject();
					if (x.getsCatalogId().equals(p.get("CatalogId").getAsString())) {
						Logger.verifyEquals(actNum, p.get("Stock").getAsInt(), "验证活动库存");
					}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogStockByProductInActivityIdList_002() {
		Logger.start(true, "查询规格活动库存,多规格报名");
		try {
			// //创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 创建一个活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);

			getcatalogstockbyproductinactivityidlistBean
					.setProductInActivityIdList(Arrays.asList(Integer.parseInt(aids.get(0))));
			getcatalogstockbyproductinactivityidlistCall.setData(getcatalogstockbyproductinactivityidlistBean);
			getcatalogstockbyproductinactivityidlistCall.callService();
			Logger.verifyEquals("200", getcatalogstockbyproductinactivityidlistCall.getString("Code"), "验证code");
			new YmtCatalogsIWapper().selectCatalogsByProductId(productId).forEach(x -> {
				JsonObject CatalogStockMap = getcatalogstockbyproductinactivityidlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonObject("CatalogStockMap");
				JsonObject p = CatalogStockMap.getAsJsonArray(aids.get(0)).get(0).getAsJsonObject();
					if (x.getsCatalogId().equals(p.get("CatalogId").getAsString())) {
						Logger.verifyEquals(actNum, p.get("Stock").getAsInt(), "验证活动库存");
					}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}



	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogStockByProductInActivityIdList_003() {
		Logger.start(true, "查询规格活动库存,ProductInActivityId为空");
		try {
			getcatalogstockbyproductinactivityidlistBean.setProductInActivityIdList(null);
			getcatalogstockbyproductinactivityidlistCall.setData(getcatalogstockbyproductinactivityidlistBean);
			getcatalogstockbyproductinactivityidlistCall.callService();
			Logger.verifyEquals("201", getcatalogstockbyproductinactivityidlistCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}